const app = getApp();
import api from "../../utils/api_util";
import tip from "../../utils/tip";
import baseUtil from "../../utils/base_util";

Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        codeText: '获取验证码',
        bingphone: '',
        msgCode: '',
        inviteCode: '',
        isLogin: false,

        //微信授权
        wxLogincode: "",
    },
    onLoad: function (options) {
    },
    // input监听手机号
    bingPhone(e) {
        this.setData({
            bingphone: e.detail.value
        })
    },
    jumpPage: function () {

    },
    //code
    bindCode: function (e) {
        this.setData({
            bingphone: e.detail.value
        })
    },
    //短信验证码
    msgCode: function (e) {
        this.setData({
            msgCode: e.detail.value
        })
    },
    //邀请码
    inviteCodeChange: function (e) {
        this.setData({
            inviteCode: e.detail.value
        })
    },

    // 获取手机发送验证码
    getPhoneCodePost: function (e) {
        let that = this;
        let bingphone = that.data.bingphone;
        let type = e.currentTarget.dataset.type;

        let isPass = /^[1][3,4,5,7,8][0-9]{9}$/.test(bingphone);
        if (!isPass) {
            wx.showToast({
                title: '手机号有误，请检查后确认',
                icon: 'none'
            });
            return
        }


        api.wxRequest.apiRequest(that, api.urls.checkWxApplet, {
            method: 'GET',
            data: {
                v: '1.0.0',
                mobile: bingphone,
            },
            success: function (res) {

                if (type == 2) {
                    if (res.data.result) {
                        that.data.loginBtn = 1;
                    } else {
                        that.data.loginBtn = 2;
                    }
                }
                api.wxRequest.apiRequest(that, api.urls.sendVerifyCode, {
                    method: 'POST',
                    data: {
                        v: '1.0.0',
                        mobile: bingphone,
                        type: 1,
                    },
                    success: function (res) {
                        that.getPhoneCode();
                    },
                });

                that.setData({
                    loginBtn: that.data.loginBtn,
                })
            },
        })

    },
    getPhoneCode: function () {
        let that = this;

        let time = 60;

        setTimeout(function () {

            tip.alert("短信验证码已发出，请稍等");

        }, 100);

        that.setData({
            codeText: '60秒后重发',
            disabled: true
        })
        var Interval = setInterval(function () {
            time--;
            if (time > 0) {
                that.setData({
                    codeText: time + '秒后可重发'
                })
            } else {
                clearInterval(Interval);
                that.setData({
                    codeText: '获取验证码',
                    disabled: false
                })
            }
        }, 1000)
    },

    //登录
    showPhoneModal: function () {

        let that = this;
        wx.redirectTo({
            url: '/pages/Login/Login'
        })

    },
    registerEvent: function () {
        let that = this;
        if(that.data.bingphone < 0 || that.data.bingphone == ''){
            tip.alert("请输入手机号~");
            return;
        }
        if(that.data.msgCode < 0 || that.data.msgCode == ''){
            tip.alert("请填写验证码~");
            return;
        }
        api.wxRequest.apiRequest(that, api.urls.userLogin, {
            method: 'POST',
            data: {
                v: '1.0.0',
                mobile: that.data.bingphone,
                code: that.data.msgCode,
                inviteCode: that.data.inviteCode,
            },
            success: function (res) {
                setTimeout(function () {
                    tip.alert("注册成功,请登录~");
                }, 100);

                wx.redirectTo({
                    url: '/pages/Login/Login'
                })
            },
            code: function (res) {
            },
        })
    }
})